using System.IO;

namespace Game.Hotfix
{
    public static class HotfixConst
    {
        public static string hotfixArgs
        {
            get
            {
                return "channel=windows&version=0";
            }
        }

        public static string hotfixUrl
        {
            get
            {
                return "http://192.168.0.107:10020";
            }
        }
        
        public static string hotfixCommand = $"{hotfixUrl}/entryHandler.launcher?{hotfixArgs}";

        public const string versionFile = "Version.txt";
        
        // 当前热更过的版本文件路径
        public static string hotfixVersionFile
        {
            get
            {
                if (File.Exists($"{FileHelper.hotfixFolder}/{versionFile}"))
                    return $"{FileHelper.hotfixFolder}/{versionFile}";

                return $"{FileHelper.streamingFolder}/{versionFile}";
            }
        }
        
        // 原始资源版本文件路径
        public static string engineVersionFile
        {
            get
            {
                return $"{FileHelper.streamingFolder}/{versionFile}";
            }
        }
        
        public static string localChecksumFile
        {
            get
            {
                string patchChecksumFile = $"{FileHelper.hotfixFolder}/{FileHelper.checksumHotfixeFile}";
                if (File.Exists(patchChecksumFile))
                {
                    return patchChecksumFile;
                }
                
                return $"{FileHelper.streamingFolder}/{FileHelper.checksumHotfixeFile}";
            }
        }
        
        public static string remoteChecksumFile
        {
            get
            {
                return FileHelper.checksumHotfixeFile;
            }
        }
        
        public static void WriteAllBytes(string path, byte[] data, int start, int count)
        {
            string dir = Path.GetDirectoryName(path);
            FileHelper.CreateDirIfNotExist(dir);
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            
            using (FileStream fp = File.OpenWrite(path))
            {
                fp.Write(data, start, count);
                fp.Flush();
                fp.Close();
            }
            
            SetFileNoBackup(path);
        }
        
        public static void SetFileNoBackup(string path)
        {
#if UNITY_IOS && !UNITY_EDITOR
            UnityEngine.iOS.Device.SetNoBackupFlag(path);
#endif
        }
    }
}